package com.doradance.view.recyclerview;

import android.support.v7.widget.RecyclerView;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.doradance.view.listener.OnPageRefreshListener;
import com.doradance.view.status.DefaultEmptyPageView;
import com.doradance.view.status.NetworkFailurePageView;
import com.doradance.view.status.RefreshPageView;
import com.doradance.view.status.StatusView;
import com.doradance.view.status.ViewConstant;
import com.yong.android.framework.view.adapter.ViewHolderLifecycle;
import com.yong.android.framework.view.recyclerview.StatusCell;


/**
 * Description:
 * Author:yong
 * Date:2017-12-21
 */

class DefaultStatusCell extends StatusCell<RecyclerView.ViewHolder> {
    private OnPageRefreshListener mOnPageRefreshListener;
    private int mStatus = ViewConstant.VIEW_STATUS_NONE;

    private SparseArray<StatusView> statusViewSparseArray = new SparseArray<>();

    public DefaultStatusCell() {
        addStatusView(new NetworkFailurePageView());
        addStatusView(new DefaultEmptyPageView());
        addStatusView(new RefreshPageView());
    }

    public void setOnPageRefreshListener(OnPageRefreshListener onPageRefreshListener) {
        this.mOnPageRefreshListener = onPageRefreshListener;
    }

    /**
     * 增加状态视图，如果增加的视图的{@link StatusView#getStatus()}与默认增加的相同，则会覆盖之前默认的配置
     *
     * @param statusView 状态视图
     */
    public void addStatusView(StatusView statusView) {
        statusViewSparseArray.put(statusView.getStatus(), statusView);
    }

    @Override
    protected RecyclerView.ViewHolder onCreateViewHolder(LayoutInflater layoutInflater, ViewGroup viewGroup, int i) {
        StatusView statusView = statusViewSparseArray.get(mStatus);
        if (statusView != null) {
            return new DefaultStatusHolder(statusView, layoutInflater, viewGroup);
        }
        return null;
    }

    @Override
    protected void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {

    }

    public void notifyStatus(int status) {
        if (status == ViewConstant.VIEW_STATUS_SUCCESS) {
            mStatus = status;
            setEnable(false);
        } else {
            mStatus = status;
            setEnable(true);
            if (status == ViewConstant.VIEW_STATUS_REFRESH) {
                if (mOnPageRefreshListener != null) {
                    mOnPageRefreshListener.onPageRefresh();
                }
            }

        }

    }

    @Override
    public int getCount() {
        return mStatus == ViewConstant.VIEW_STATUS_NONE || mStatus == ViewConstant.VIEW_STATUS_SUCCESS ? 0 : 1;
    }

    @Override
    public int getItemViewType(int position) {
        return mStatus;
    }

    private static final class DefaultStatusHolder extends RecyclerView.ViewHolder implements ViewHolderLifecycle {
        private StatusView mStatusView;

        public DefaultStatusHolder(StatusView statusView, LayoutInflater inflater, ViewGroup parent) {
            super(statusView.onCreateStatusView(inflater, parent));
            mStatusView = statusView;
            mStatusView.onStatusViewCreated();
        }

        @Override
        public void onViewAttachedToWindow() {
            mStatusView.onViewAttachedToWindow();
        }

        @Override
        public void onViewDetachedFromWindow() {
            mStatusView.onViewDetachedFromWindow();
        }
    }

}

